package com.hx.mq;

import com.hx.dto.MoodDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.io.Serializable;

/**
 * 消息消费者 通常都要实现消息监听器接口
 * MessageListener
 */
@Component
@Slf4j
public class MoodConsumer implements MessageListener {

    @Resource
    private RedisTemplate redisTemplate;

    //用于存放不同说说id的redis(HashSet)的主键key,value对应不同的说说id
    private static final String PRAISE_HASH_KEY = "praise.mood.id.list.key";

    /**
     * 监听消息 做出消费行为
     * @param message
     */
    @Override
    public void onMessage(Message message) {

        try{
            System.out.println(".................................");
            //转化为实体类
            ActiveMQObjectMessage mqObjectMessage = (ActiveMQObjectMessage) message;
            MoodDTO moodDTO = (MoodDTO) mqObjectMessage.getObject();

            //将 moodId 存放在redis中进行消费
            redisTemplate.opsForSet().add(PRAISE_HASH_KEY,moodDTO.getId());
            //将 userId 存放在moodId中进行消费
            redisTemplate.opsForSet().add(moodDTO.getId(),moodDTO.getUserId());

            log.info("消费者--->>>用户id:{},说说id{},进行消费。",moodDTO.getUserId(),moodDTO.getId());
            System.out.println("消费者------------------>>>");

        }catch(Exception e){

            System.out.println(e.getMessage());

        }

    }
}
